package cn.com.gome.generator.controller.creators;

import cn.com.gome.framework.dao.entity.TblProjectBasicInfo;
import cn.com.gome.framework.dao.mapper.ser.TblProjectBasicInfoSerMapper;
import cn.com.gome.generator.service.DataSourceCreateService;
import com.gomeplus.frame.controller.AbstractAdminController;
import com.gomeplus.frame.logic.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: DataSourcePropsCreateController
 * @Description: 生成数据源的基本信息
 * Created by wangheming on 2017/12/1 15:06.
 */
@Controller
@RequestMapping("/dataSourceCreate")
public class DataSourcePropsCreateController extends AbstractAdminController {

    @Autowired
    private TblProjectBasicInfoSerMapper tblProjectBasicInfoSerMapper;

    @Autowired
    private DataSourceCreateService dataSourceCreateService;
    /**
     * 生成数据库文件Ajax返回提示信息
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/create")
    public void create(HttpServletRequest request, HttpServletResponse response, Model model){
        try {
            model.addAttribute("resultStatus", "false");

            TblProjectBasicInfo projectCode = tblProjectBasicInfoSerMapper.query(request.getParameter("projectCode"));

            if (null == projectCode) {
                ajaxJsonMessage(response,"0002","温馨提示：没有找到符合条件的记录！","");
            } else {
                // TODO 执行Logic生成指定路径的文档
                //request.getRealPath("/");//不推荐使用获取工程的根路径
                //request.getRealPath(request.getRequestURI());//获取jsp的路径，这个方法比较好用，可以直接在servlet和jsp中使用
                //this.getClass().getClassLoader().getResource("").getPath();//获取工程classes 下的路径，这个方法可以在任意jsp，servlet，java文件中使用，因为不管是jsp，servlet其实都是java程序，都是一个 class。所以它应该是一个通用的方法。
                String realPath = request.getSession().getServletContext().getRealPath("/");//获取工程的根路径，这个方法比较好用，可以直接在servlet和jsp中使用

                Map basicInfo = new HashMap<String,Object>();
                basicInfo.put("projectBasicInfo",projectCode);
                basicInfo.put("realPath",realPath);

                if (dataSourceCreateService.service(basicInfo)){
                    ajaxJsonMessage(response,"0000","温馨提示：生成成功！，重启应用后生效","");
                }
                ajaxJsonMessage(response,"0002","温馨提示：生成失败","");
            }
        } catch (Exception e) {
            ajaxJsonMessage(response,"0002","温馨提示：生成失败！","");
        }
    }

}
